package com.shop.loan.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shop.loan.mapper.LoanOrderAuthByMybatisMapper;
import com.shop.loan.mapper.LoanOrderByMybatisMapper;
import com.shop.loan.mapper.UserByMybatisMapper;
import com.shop.loan.model.entity.*;
import com.shop.loan.model.enums.OrderStatus;
import com.shop.loan.model.enums.ServiceType;
import com.shop.loan.model.vo.LoanOrderAuthVo;
import com.shop.loan.service.AsyncService;
import com.shop.loan.service.EruptUserDataService;
import com.shop.loan.service.MsgService;
import com.shop.loan.service.SettingService;
import com.shop.loan.util.SysMsgUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import xyz.erupt.annotation.fun.OperationHandler;
import xyz.erupt.upms.service.EruptUserService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderSignCreateHandler implements OperationHandler<Object, LoanOrderAuthVo> {

    @Resource
    LoanOrderByMybatisMapper loanOrderByMybatisMapper;

    @Resource
    EruptUserService eruptUserService;

    @Resource
    LoanOrderAuthByMybatisMapper loanOrderAuthByMybatisMapper;

    @Resource
    SysMsgUtil sysMsgUtil;

    @Resource
    UserByMybatisMapper userByMybatisMapper;

    @Resource
    SettingService settingService;

    @Resource
    EruptUserDataService eruptUserDataService;

    @Resource
    MsgService msgService;

    @Lazy
    @Resource
    AsyncService asyncService;

    @Override
    public String exec(List<Object> data, LoanOrderAuthVo loanOrderAuthVo, String[] param) {
        Object item = data.get(0);
        JSONObject dataJs = JSON.parseObject(JSON.toJSONString(item));
        Long orderId = dataJs.getLongValue("id");

        LoanOrderByMybatis loanOrderByMybatis = new LoanOrderByMybatis();
        loanOrderByMybatis.setId(orderId);
        if (loanOrderAuthVo.getType() == 1) {
            loanOrderByMybatis.setStatus(OrderStatus.wait_loan.getStatus());

            SettingByMybatis setting = settingService.getSetting();
            //是否自动分配放款客服
            if (setting.getAutoAllocLoanService() != null && setting.getAutoAllocLoanService()) {
                List<LoanOrderByMybatis> orderByMybatisList = new ArrayList<>();
                orderByMybatisList.add(loanOrderByMybatisMapper.selectByPrimaryKey(orderId));
                //订单列表分配放款客服
                eruptUserDataService.allotServiceByList(orderByMybatisList, ServiceType.loan_service);
            } else {
                //通知放款客户
                msgService.newMsgByOrder(NewMsgType.loan);
            }
        } else {
            //审核拒绝
            loanOrderByMybatis.setStatus(OrderStatus.refuse.getStatus());

            //给用户发送消息
            UserByMybatis userByMybatis = userByMybatisMapper.selectByOrderId(orderId);
            if (userByMybatis != null) {
                sysMsgUtil.sendAuthFail(userByMybatis.getMobile());
                if (loanOrderAuthVo.getUserStatus() != null) {
                    UserByMybatis updateUser = new UserByMybatis();
                    updateUser.setId(userByMybatis.getId());
                    updateUser.setMachineStatus(loanOrderAuthVo.getUserStatus());
                    userByMybatisMapper.updateByPrimaryKeySelective(updateUser);
                }
            }
        }
        loanOrderByMybatisMapper.updateByPrimaryKeySelective(loanOrderByMybatis);
        //增加一条审核记录
        LoanOrderAuthByMybatis orderAuthLog = new LoanOrderAuthByMybatis();
        orderAuthLog.setAuthStatus(loanOrderAuthVo.getType() == 1);
        orderAuthLog.setOrderId(orderId);
        orderAuthLog.setAuthBy(eruptUserService.getCurrentEruptUser().getName());
        orderAuthLog.setReason(loanOrderAuthVo.getReason());
        orderAuthLog.setCreateTime(new Date());
        orderAuthLog.setOrderStatus(loanOrderByMybatis.getStatus());
        loanOrderAuthByMybatisMapper.insertSelective(orderAuthLog);

        asyncService.notifyCheck(orderId);
        return null;
    }
}
